﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Xss.Message
{
    /// <summary>
    /// 消息上下文
    /// </summary>
    [Serializable]
    public class MessageContext
    {
        #region 构造

        /// <summary>
        /// 默认构造
        /// </summary>
        public MessageContext()
        {
            Messages = new List<string>();
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        /// <param name="msg">添加消息</param>
        public MessageContext(string msg)
            : this()
        {
            AddMessage(msg);
        }

        #endregion

        #region 属性

        /// <summary>
        /// 消息列表
        /// </summary>
        public List<string> Messages { get; set; }
        #endregion

        #region 方法

        /// <summary>
        /// 添加消息
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        public MessageContext AddMessage(string msg)
        {
            Messages.Add(msg);
            return this;
        }

        /// <summary>
        /// 包含消息的时候抛出异常信息
        /// </summary>
        public void ThrowException()
        {
            if (Messages.Count > 0)
            {
                var message = string.Join(System.Environment.NewLine, Messages.ToArray());
                throw new Exception(message);
            }
        }

        /// <summary>
        /// 清空消息
        /// </summary>
        public void ClearMessage()
        {
            Messages.Clear();
        }
        #endregion
    }
}
